﻿
using HDSmartCommunity.BUS;
using HDSmartCommunity.DAL;
using HDSmartCommunity.DevicePlatform;
using HDSmartCommunity.Models;
using HDSmartCommunity.Serial;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HDSmartCommunity.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ElevatorInfoController : ControllerBase
    {
        public DeviceSerialData serial = SerialServer.serial;
        public ElevatorInfoController()
        {
        }

        [HttpPost]
        [HttpGet]
        [Route("GetAllFloor")]
        public ResponseMessage GetAllFloor()
        {
            ResponseMessage message = new ResponseMessage();
            FloorInfoDAL da = new FloorInfoDAL();
            try
            {
                List<FloorInfo> list = da.GetAllFloor();
                message.errcode = 0;
                message.data = list;
                message.total = list.Count;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "获取楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        [HttpPost]
        [Route("GetFloorByID")]
        public ResponseMessage GetFloorByID(FloorInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            FloorInfoDAL da = new FloorInfoDAL();
            try
            {
                FloorInfo list = da.GetFloorByID(info.ID);
                message.errcode = 0;
                message.data = list;
                message.total = 1;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "载入楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        [HttpPost]
        [Route("UpdateFloorInfoByID")]
        public ResponseMessage UpdateFloorInfoByID(FloorInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            FloorInfoDAL da = new FloorInfoDAL();
            try
            {
                bool result = da.UpdateFloorInfoByID(info);
                if (result)
                {
                    serial.act_list.Clear();
                    serial.phy_list.Clear();
                    serial.show_list.Clear();
                    List<FloorInfo> floorInfo = serial.GetAllFloor();
                    foreach (var item in floorInfo)
                    {
                        serial.phy_list.Add(item.physicsfloor);
                        serial.show_list.Add(item.displayfloor);
                        serial.act_list.Add(item.actualfloor);
                    }
                    message.errcode = 0;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "更新楼层信息失败";
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "更新楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        [HttpPost]
        [Route("InsertFloorInfo")]
        public ResponseMessage InsertFloorInfo(FloorInfo info)
        {
            ResponseMessage message = new ResponseMessage();
            FloorInfoDAL da = new FloorInfoDAL();
            try
            {
                bool result = da.InsertFloorInfo(info);
                if (result)
                {
                    serial.act_list.Clear();
                    serial.phy_list.Clear();
                    serial.show_list.Clear();
                    List<FloorInfo> floorInfo = serial.GetAllFloor();
                    foreach (var item in floorInfo)
                    {
                        serial.phy_list.Add(item.physicsfloor);
                        serial.show_list.Add(item.displayfloor);
                        serial.act_list.Add(item.actualfloor);
                    }

                    message.errcode = 0;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "新增楼层信息失败";
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "新增楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        [HttpPost]
        [Route("UpdateFloorInfo")]
        public ResponseMessage UpdateFloorInfo(List<FloorInfo> list)
        {
            ResponseMessage message = new ResponseMessage();
            string error = string.Empty;
            FloorInfoDAL da = new FloorInfoDAL();
            string ids = string.Empty;
            try
            {
                List<FloorInfo> floors = da.GetAllFloor();

                foreach (var item in list)
                {
                    if (item.ID != 0)
                    {
                        if (!da.UpdateFloorInfoByID(item))
                        {
                            error += item.physicsfloor + ",";
                        }
                        else
                        {
                            ids += item.ID + ",";
                        }
                    }
                    else
                    {
                        if (!da.InsertFloorInfo(item))
                        {
                            error += item.physicsfloor + ",";
                        }
                    }
                }
                string delete_ids = string.Empty;
                foreach (var item in floors)
                {
                    string[] arry = ids.Split(',');
                    if (!arry.Contains(item.ID.ToString()))
                    {
                        delete_ids += item.ID + ",";
                    }
                }
                if (!string.IsNullOrEmpty(delete_ids))
                {
                    delete_ids = delete_ids.Substring(0, delete_ids.Length - 1);
                    da.DeleteFloorInfo(delete_ids);
                }

                serial.act_list.Clear();
                serial.phy_list.Clear();
                serial.show_list.Clear();
                List<FloorInfo> floorInfo = da.GetAllFloor();
                foreach (var floor_item in floorInfo)
                {
                    serial.phy_list.Add(floor_item.physicsfloor);
                    serial.show_list.Add(floor_item.displayfloor);
                    serial.act_list.Add(floor_item.actualfloor);
                }

                if (!string.IsNullOrEmpty(error))
                {
                    message.errcode = 1;
                    message.errmsg = "物理楼层" + error.TrimEnd(',') + "更新失败";
                }
                else
                {
                    message.errcode = 0;
                    message.total = 1;
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "更新楼层信息异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

    }
}
